package com.heatup.apt.management.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.common.dao.DataCenterMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.management.service.datacenter.DataCenterService;
import com.heatup.apt.management.service.store.StoreService;
import com.heatup.apt.model.store.StoreInfo;
import com.heatup.apt.model.store.StoreInfoRequest;


@Controller
@RequestMapping("system/data")
public class DataCenterController extends BaseController{
	
	private final static Logger logger= LoggerFactory.getLogger(DataCenterController.class);
	
	@Autowired
	private DataCenterService  dataCenterService;
	@Autowired
	private StoreService  storeService;
	@Autowired 
	private DataCenterMapper dataCenterMapper;

	/**
	 * 跳转页面时的数据查询
	 * @return
	 */
    @RequestMapping(value = "/to_data_center")
    public ModelAndView toDataCenter() {
    	ModelAndView model = new ModelAndView("data/data-center");
    	//当前日期字符串
    	String nowDate = DateUtils.format(new Date(),"yyyy-MM-dd");
    	String[] lastWeekday = DateUtils.getLastTimeInterval().split(",");
    	//上周开始日期
    	String lastWeekStartDay=lastWeekday[0];
    	//上周结束日期
    	String lastWeekEndDay = lastWeekday[1];
    	//上个月份
    	String lastMonth = DateUtils.getLastMonthInterval();
    	Map<String,Object> paraMap= new HashMap<String,Object>();
    	paraMap.put("nowDate", nowDate);
    	paraMap.put("lastWeekStartDay", lastWeekStartDay);
    	paraMap.put("lastWeekEndDay", lastWeekEndDay);
    	paraMap.put("lastMonth", lastMonth);
    	
    	Map<String,Object> respMap = dataCenterService.countDateCenterService(paraMap);
    	respMap.put("nowDate", nowDate);
    	respMap.put("lastWeekStartDay", lastWeekStartDay);
    	respMap.put("lastWeekEndDay", lastWeekEndDay);
    	respMap.put("lastMonth", lastMonth);
    	String daterange = nowDate+"~"+nowDate;
    	respMap.put("daterange", daterange);
    	
    	model.addAllObjects(respMap);
    	
        return model;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/to_data_store_list")
    public ModelAndView toDataCenterStoreList() {
    	ModelAndView model = new ModelAndView("data/data_store_list");
    	List<StoreInfoRequest> selfStoreList = storeService.queryDataCenterStoreList("2","1");
    	List<StoreInfoRequest> joinStoreList = storeService.queryDataCenterStoreList("2","2");
    	Map<String,Object> respMap = new HashMap<String,Object>();
    	respMap.put("selfStoreList", selfStoreList);
    	respMap.put("selfStoreListCount", selfStoreList.size());
    	respMap.put("joinStoreList", joinStoreList);
    	respMap.put("joinStoreListCount", joinStoreList.size());
    	model.addAllObjects(respMap);
    	return model;
    }
    
    @RequestMapping(value="/to_data_store")
    public ModelAndView toDataStore(HttpServletRequest request,HttpServletResponse response) {
    	Integer storeId = RequestTools.RequestInt(request, "storeId", 0);
    	String storeName = RequestTools.RequestString(request, "storeName","");
    	StoreInfo storeInfo = storeService.queryStoreInfoById(Long.valueOf(storeId));
    	ModelAndView model = new ModelAndView("data/data_store");
    	Map<String,Object> respMap = new HashMap<String,Object>();
    	if(storeId > 0){
    		Integer bool = dataCenterMapper.queryStoreIsHaveCard(storeId);
    		if(bool > 0){
    			respMap.put("monthCard", bool);
    		}
    	}
    	respMap.put("storeId", storeId);
    	respMap.put("storeName", storeInfo.getStoreName());
    	respMap.put("storeInfo", storeInfo);
    	model.addAllObjects(respMap);
    	return model;
    }
    
    /**
     * 按日期区间条件查询数据
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/query_data_center_days")
    public void queryDataCenterByDays(HttpServletResponse response ,@RequestParam String daterange){
		String startDay = null;
		String endDay = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDay = dateranges[0];
			endDay = dateranges[1];
		}
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("startDay", startDay);
    	paramMap.put("endDay", endDay);
    	Map<String,Object> resp =  dataCenterService.countDateCenterByDaysService(paramMap);
    					   resp.put("daterange", daterange);				   
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 按月份查询数据
     * @param response
     * @param yearMonth
     */
    @ResponseBody
    @RequestMapping(value="/query_data_yearmonth")
    public void queryDataByYearMonth(HttpServletResponse response ,@RequestParam String yearMonth){
    	Map<String,Object> resp = dataCenterService.queryDataByYearMonth(yearMonth);
    	resp.put("lastMonth", yearMonth);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 按区间时间查询教练重复购买率排名
     */
    @ResponseBody
    @RequestMapping(value="/query_data_coach_sort")
    public void queryCoachSortDataByDaterange(HttpServletResponse response ,@RequestParam String sort_datarange1){
    	String startDay = null;
		String endDay = null;
		if (sort_datarange1 != null || "".equals(sort_datarange1)) {
			String[] dateranges = sort_datarange1.split("~");
			startDay = dateranges[0];
			endDay = dateranges[1];
		}
		
		Map<String,Object> resp = dataCenterService.queryCoachSortDataByDateRange(startDay,endDay);
		this.responseSendMsg(response, JSONObject.toJSONString(resp));
		
    }
    
    /**
     * 按区间时间查询课程重复购买率排名
     */
    @ResponseBody
    @RequestMapping(value="/query_data_course_sort")
    public void queryCourseSortDataByDaterange(HttpServletResponse response ,@RequestParam String sort_datarange2){
    	String startDay = null;
		String endDay = null;
		if (sort_datarange2 != null || "".equals(sort_datarange2)) {
			String[] dateranges = sort_datarange2.split("~");
			startDay = dateranges[0];
			endDay = dateranges[1];
		}

		Map<String,Object> resp = dataCenterService.queryCourseSortDataByDateRange(startDay,endDay);
		this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    
    /**
     * 按区间时间查询课程满课率排名
     */
    @ResponseBody
    @RequestMapping(value="/query_team_full_class_rate_sort")
    public void queryTeamFullClassRateSort(HttpServletResponse response ,@RequestParam String sort_datarange3){
    	String startDay = null;
		String endDay = null;
		if (sort_datarange3 != null || "".equals(sort_datarange3)) {
			String[] dateranges = sort_datarange3.split("~");
			startDay = dateranges[0];
			endDay = dateranges[1];
		}

		Map<String,Object> resp = dataCenterService.queryTeamFullClassRateSort(startDay,endDay);
		this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 查询周日期数据
     * @param response
     * @param weekdaterange
     */
    @ResponseBody
    @RequestMapping(value="/query_data_weekdaterange")
    public void queryDataWeekDaterange(HttpServletResponse response, @RequestParam String weekdaterange){
    	String startDate = null;
		String endDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		if (weekdaterange != null || "".equals(weekdaterange)) {
			String[] dateranges = weekdaterange.split("-");
			try{
				Date startDay = sdf.parse(dateranges[0]);
				Date endDay = sdf.parse(dateranges[1]);
				startDate =sdf1.format(startDay);
				endDate  = sdf1.format(endDay);
			}catch(Exception e){
				logger.info("日期类型转换错误"+ e.getMessage());
			}
		}	
		Map<String,Object> resp = dataCenterService.queryDataByWeekRange(startDate, endDate);
		resp.put("startDate", startDate);
		resp.put("endDate", endDate);
		
		this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 查询实收金额明细
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/queryCountAmtDetail")
    public void queryCountAmtDetail(HttpServletResponse response ,@RequestParam String daterange){
    	String startDate = null;
    	String endDate = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDate = dateranges[0];
			endDate = dateranges[1];
		}
    	Map<String,Object> resp = dataCenterService.queryCountAmtDetail(startDate,endDate);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    /**
     * 查询订单数量明细
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/queryOrderCountDetail")
    public void queryOrderCountDetail(HttpServletResponse response ,@RequestParam String daterange){
    	String startDate = null;
    	String endDate = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDate = dateranges[0];
			endDate = dateranges[1];
		}
    	Map<String,Object> resp = dataCenterService.queryOrderCountDetail(startDate,endDate);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    
    /**
     * 查询新增会员数量明细
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/queryNewVipCountDeatil")
    public void queryNewVipCountDeatil(HttpServletResponse response ,@RequestParam String daterange){
    	String startDate = null;
    	String endDate = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDate = dateranges[0];
			endDate = dateranges[1];
		}
    	Map<String,Object> resp = dataCenterService.queryNewVipCountDeatil(startDate,endDate);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    
    /**
     * 查询团课满课率明细
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/queryTeamFullClassRateDetail")
    public void queryTeamFullClassRateDetail(HttpServletResponse response ,@RequestParam String daterange){
    	String startDate = null;
    	String endDate = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDate = dateranges[0];
			endDate = dateranges[1];
		}
    	Map<String,Object> resp = dataCenterService.queryTeamFullClassRateDetail(startDate,endDate);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 查询团课周排课量明细
     * @param response
     * @param start_weekrange
     * @param end_weekrange
     */
    @ResponseBody
    @RequestMapping(value="/queryWeekCourseCountDetail")
    public void queryWeekCourseCountDetail(HttpServletResponse response ,@RequestParam String start_weekrange,@RequestParam String end_weekrange){
    	Map<String,Object> resp = dataCenterService.queryWeekCourseCountDetail(start_weekrange,end_weekrange);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    /**
     * 查询零售复购率明细
     * @param response
     * @param daterange
     */
    @ResponseBody
    @RequestMapping(value="/queryRetailRepeatChargeRateDetail")
    public void queryRetailRepeatChargeRateDetail(HttpServletResponse response ,@RequestParam String daterange){
    	String startDate = null;
    	String endDate = null;
		if (daterange != null || "".equals(daterange)) {
			String[] dateranges = daterange.split("~");
			startDate = dateranges[0];
			endDate = dateranges[1];
		}
    	Map<String,Object> resp = dataCenterService.queryRetailRepeatChargeRateDetail(startDate,endDate);
    	this.responseSendMsg(response, JSONObject.toJSONString(resp));
    }
    
    
   

    
  
}
